package com.shuaixin.web.controller;

import com.shuaixin.web.model.Message;
import com.shuaixin.web.rebackmodel.InfoBack;
import com.shuaixin.web.rebackmodel.addMessageBack;
import com.shuaixin.web.service.MessageServiceI;
import com.shuaixin.web.util.ErrorCodeUtils;
import com.shuaixin.web.util.FastJsonTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by wby on 2015/5/21.
 */
@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageServiceI messageService;

    @ResponseBody
    @RequestMapping("/send")
    public addMessageBack sendMessage(@RequestParam(value = "uuid") String uid,
                                      @RequestParam(value = "name") String name,
                                      @RequestParam(value = "address") String address,
                                      @RequestParam(value = "lon") String lon,
                                      @RequestParam(value = "lat") String lat,
                                      @RequestParam(value = "content") String content) {
        Message message = new Message();
        double lon1 = Double.valueOf(lon);
        double lat1 = Double.valueOf(lat);
        message.setUid(uid);
        message.setName(name);
        message.setAddress(address);
        message.setLat(lat1);
        message.setLon(lon1);
        message.setContent(content);
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        message.setCreatedate(dateNowStr);
        //img、imgid先写死
        String url = "https://coding.net/u/youzi/p/CodingServer/git/raw/master/apkfolder/4949.jpg";
        message.setImg(url);
        message.setImgid(116);

        String id = messageService.addMessage(message);
        String str = FastJsonTool.createJsonString(message);
        addMessageBack messageBack = new addMessageBack();
        if (id.isEmpty() || id.length() <= 0) {
            messageBack.setErrcode(ErrorCodeUtils.EC_UNKNOWN);
        } else {
            messageBack.setSuccess(true);
            messageBack.setErrcode(ErrorCodeUtils.EC_SUCCESS);
        }
        return messageBack;
    }


    public final static String curPageDefaultValue = "1";
    public final static String curRowsDefaultValue = "5";

    @ResponseBody
    @RequestMapping("/getInfo")
    public InfoBack getInfo(@RequestParam(value = "page", defaultValue = curPageDefaultValue) int page,
                            @RequestParam(value = "rows", defaultValue = curRowsDefaultValue) int rows,
                            @RequestParam(value = "lon") double lon,
                            @RequestParam(value = "lat") double lat) {
        double distance;
        distance = 1000;

        List<Message> messagesList = messageService.getMessage(page, rows, lon, lat, distance);
        Long count = messageService.getMessageCount(lon, lat, distance);

        InfoBack infoBack = new InfoBack();
        infoBack.setCurPage(page);
        infoBack.setCurRows(rows);
        infoBack.setTotal(count.intValue());
        infoBack.setMessageList(messagesList);
        infoBack.setSuccess(true);
        infoBack.setErrcode(ErrorCodeUtils.EC_SUCCESS);
        return infoBack;
    }


    @ResponseBody
    @RequestMapping("/neabyMsg")
    public InfoBack neabyMsg(@RequestParam(value = "lon") String lon,
                            @RequestParam(value = "lat") String lat) {

        double distance;
        double lon1 = Double.parseDouble(lon);
        double lat1 = Double.parseDouble(lat);

        distance = 5000;

        List<Message> messagesList = messageService.getgetNeabyInfoMessage(lon1, lat1, distance);
        System.out.println(messagesList.toString());

        InfoBack infoBack = new InfoBack();
        infoBack.setCurPage(10);
        infoBack.setCurRows(10);
        infoBack.setTotal(10);
        infoBack.setMessageList(messagesList);
        infoBack.setSuccess(true);
        infoBack.setErrcode(ErrorCodeUtils.EC_SUCCESS);
        return infoBack;
    }

}
